package it.okkam.geosparql;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;

import it.okkam.Changes.EventoCambiamento;
import it.okkam.costants.ExtentMaxLatComune;
import it.okkam.strutturedati.Extent;
import it.okkam.strutturedati.ExtentCcc;
import it.okkam.utility.Utility;
import it.okkam.utility.UtilityQuery;

import org.geospatialweb.arqext.Geo;
import org.geospatialweb.arqext.Indexer;

import com.hp.hpl.jena.graph.query.Query;
import com.hp.hpl.jena.query.Dataset;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Model;

import com.hp.hpl.jena.tdb.TDBFactory;

public class GeoSparqlQuery {
	
	public static HashMap<String, Dataset> dataSetComuni = new HashMap<String, Dataset>();
		
	
	public static void main(String[] args) {
		GeoSparqlQuery geo = new GeoSparqlQuery();
		UtilityQuery utq = new UtilityQuery();
		GeoGetExtentComuni geoextComuni = new GeoGetExtentComuni();
		GeoGetExtentsParticelleComuniInterressati geoInterressati = new GeoGetExtentsParticelleComuniInterressati();
		GeoGetExtentParticelleCandidate geoParticelleCandidate= new GeoGetExtentParticelleCandidate();
		HashMap<String, HashMap<String , List<Extent>>> extInteressati = new HashMap<String, HashMap<String , List<Extent>>>();  
		
		System.out.println(utq.getData()+ " Processing query");
		String grafoExtent = "http://www.okkam.org/provinciale/extents";
		String grafoCambiamenti = "http://www.okkam.org/provinciale/cambiamenti";
		
		
		
		Dataset dsProvinciale = TDBFactory.assembleDataset("/home/okkam/Nicola/assemblers/provinciale-assembler.ttl");
		
		dataSetComuni.put("provinciale", dsProvinciale);
		Model modelCambiamenti = dsProvinciale.getNamedModel(grafoCambiamenti);
		Model modelExtent = dsProvinciale.getNamedModel(grafoExtent);
		HashMap<String,Extent> extentsComuni =geoextComuni.getExtentsComuni(modelExtent);
		GeoGetSetParticelle geoSet = new GeoGetSetParticelle();
		//HashMap<String,Extent> extentsParticelle = geo.getExtentsParticelle(modelExtent, ccc);
		 Map<String, Set<String>> comuniCandidati = geo.getComuniCandidati(extentsComuni,modelCambiamenti);
		 extInteressati = geoInterressati.getExtentsParticelleComuniInterressati(comuniCandidati);

//		 Iterator iter = extInteressati.keySet().iterator();
//		 while (iter.hasNext()){
//			 String id = (String)iter.next();
//			 geoParticelleCandidate.getExtentsParticelleCandidate(id , extInteressati);
//		 }
//		 
		 HashMap<String,List<ExtentCcc>> risultato = geoParticelleCandidate.getParticelleCandidate(extInteressati);
		 //Map<String, Set<String>> particelleIndagate = null;
		 geoSet.getParticelle(risultato);
		 System.out.println(utq.getData()+ " Query done");
	}
	
	
	public static void loadDataSetComuni (String comuneCodice){
		UtilityQuery utq = new UtilityQuery();
		String comune = utq.getComune(comuneCodice); 
		if (!dataSetComuni.containsKey(comune)){
			Dataset dsComune = TDBFactory.assembleDataset("/home/okkam/Nicola/assemblers/"+comune.toLowerCase()+"-assembler.ttl");
			dataSetComuni.put(comune, dsComune);
		}
		}
	

	
	
	public Map<String, Set<String>> getComuniCandidati(HashMap<String,Extent> extentsComuni,Model modelCambiamenti){
		Indexer index = Indexer.createDefaultIndexer();
		index.createIndex(modelCambiamenti);
		//for(int i =0;i< extentsComuni.size();i++){
			//String extent = extentsComuni.get(key).getxMin()+" "+extentsComuni.get(key).getxMax()+" "+extentsComuni.get(key).getyMin()+" "+extentsComuni.get(key).getyMax();
			UtilityQuery utq = new UtilityQuery();
			GeoStringQuery geos = new GeoStringQuery();
        	Map<String, Set<String>> cambComuni = new HashMap<String, Set<String>>();
        	

			Iterator k = extentsComuni.keySet().iterator();
			while (k.hasNext()) {
				Set<String> setCambiamenti = new HashSet();
				String key = (String) k.next();
				String extent = utq.getExtentString(extentsComuni, key);
				String queryString = geos.getStringPointsInExtent(extent);
				com.hp.hpl.jena.query.Query query = QueryFactory.create(queryString);
				QueryExecution qexec = QueryExecutionFactory.create(query, modelCambiamenti);
				Geo.setContext(qexec, index);
				ResultSet results = qexec.execSelect();
				for (; results.hasNext();) {
					String risultato = results.next().toString();
					setCambiamenti.add(utq.getIDString(risultato));
				}
				cambComuni.put(key, setCambiamenti);
			}
		    Map<String, Set<String>> result = utq.getSetOrderedByCambiamentoComuni(cambComuni);
		    return result;
		    
	}
	
	
	
	
	
	
	
	
	
	

	






}
